package data;

import javax.annotation.processing.Filer;
import java.io.*;
import java.sql.*;
import java.util.*;

import static java.util.Collection.*;

public class RecordOrShowRecord {
    String dir;
    File file;
    String tableName;

    List<List<Data>> data;
    int heroNumber = 3;   //英雄榜显示的最多英雄数目

    public RecordOrShowRecord() throws IOException {
        dir = new String("src/data.txt");
        file = new File(dir);
        try {
            if (!file.exists()) {
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                writer.write("0\n0\n0\n");
                writer.flush();
                writer.close();
            } else {
                System.out.println("文件已存在");
                String line;
                BufferedReader reader = new BufferedReader(new FileReader(file));

            }
        } catch (IOException e) {
            System.out.println("发生IO异常：" + e.getMessage());
        }


        BufferedReader reader = new BufferedReader(new FileReader(file));
        data = new ArrayList<>();
        for (int i = 0; i < 3; ++i) {
            data.add(new ArrayList<>());
            String line = reader.readLine();
            if (line == null) line = "0";
            int n = Integer.parseInt(line);
            for (int j = 0; j < n; ++j) {
                line = reader.readLine();
                String[] strs = new String[2];
                strs = line.split(" ");
                Data d = new Data(strs[0], Integer.parseInt(strs[1]));
                data.get(i).add(d);
            }
        }
        reader.close();
    }

    public void setTable(String str) {
        tableName = str;
    }

    public boolean addRecord(int grade, String name, int time) throws IOException {
        boolean ok = true;
        if (tableName == null)
            ok = false;
        //检查 time 是否达到标准 (进入前 heroNumber 名)，见后面的 verifyScore 方法：
        int amount = verifyScore(grade, time);
        if (amount >= heroNumber) {
            ok = false;
        } else {
            data.get(grade).add(new Data(name, time));
            Collections.sort(data.get(grade));
            File file = new File(dir);
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            for (int i = 0; i < 3; ++i) {
                writer.write(data.get(i).size() + "\n");
                for (Data d : data.get(i)) {
                    writer.write(d.name + " " + d.time + "\n");
                }
            }
            writer.flush();
            writer.close();
        }
        return ok;
    }

    public int gradeId(String s) {
        if (s.equals("初级")) return 0;
        else if (s.equals("中级")) return 1;
        return 2;
    }

    public List<Data> queryRecord() throws IOException {
        file = new File(dir);
        try {
            if (!file.exists()) {

            } else {
                System.out.println("文件已存在");
                String line;
                BufferedReader reader = new BufferedReader(new FileReader(file));

            }
        } catch (IOException e) {
            System.out.println("发生IO异常：" + e.getMessage());
        }


        BufferedReader reader = new BufferedReader(new FileReader(file));
        data = new ArrayList<>();
        for (int i = 0; i < 3; ++i) {
            data.add(new ArrayList<>());
            String line = reader.readLine();
            if (line == null) line = "0";
            int n = Integer.parseInt(line);
            for (int j = 0; j < n; ++j) {
                line = reader.readLine();
                String[] strs = new String[2];
                strs = line.split(" ");
                Data d = new Data(strs[0], Integer.parseInt(strs[1]));
                data.get(i).add(d);
            }
        }
        reader.close();
        List<Data> record = data.get(gradeId(tableName));
        return record;
    }

    private void connectDatabase() {


    }

    private int verifyScore(int grade, int time) {

        int amount = 0;

        for (Data d : data.get(grade)) {
            if (d.time <= time) ++amount;
        }

        return amount;
    }
}
